﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nina.Examples
{
    /// <summary>
    /// Protocol handler for the server
    /// </summary>
    class ServerProtocolHandler : NinaProtocolHandler
    {
        private int m_count = 1;

        /// <summary>
        /// Called when a message is received from a client
        /// </summary>
        /// <param name="s">NinaSession from which the message was received</param>
        /// <param name="data">The data received</param>
        public override void MessageReceived(NinaSession s, byte[] data)
        {
            /* Print out received message */
            Console.WriteLine("Server received a message: " + Encoding.UTF8.GetString(data));

            /* Respond to message */
            byte[] response = Encoding.UTF8.GetBytes("Hello client (Message " + m_count.ToString() + ")");
            s.Write(response);
            m_count++;
        }

        /// <summary>
        /// Called when a session is closed
        /// </summary>
        /// <param name="s">The session which was closed</param>
        public override void SessionClosed(NinaSession s)
        {
        }

        /// <summary>
        /// Called when a session is opened
        /// </summary>
        /// <param name="s">The session which was opened</param>
        public override void SessionOpened(NinaSession s)
        {
        }
    }
}
